import { MainResponseType, buyListType } from "@/common/model/response";
import request from "@/utils/axios";

const URL = {
  get_HangSalesMarket: "Ctoc/get_HangSalesMarket", //挂卖市场
  buy_HangSalesMarketOrder: "Ctoc/buy_HangSalesMarketOrder", //购买   （挂卖订单）
  hangForSale: "Ctoc/hangForSale", //出售
  get_UserTransactionOrder: "Ctoc/get_UserTransactionOrder", //用户订单

  getTansferList: "swap/getTansferList", //我当前的交易清单
  getList: "swap/getList", //最新卖单
  getApiToken: "api/getApiToken", //getApiToken
  swap: "swap/swap", //交易
  getCurrencyList: "swap/getCurrencyList", //获取货币列表
};

export function get_UserTransactionOrder(page: string, type: number) {
  return request.post<any>(URL.get_UserTransactionOrder, {
    page,
    type,
  });
}

export function hangForSale(nums: string, password: string, APITOKEN: string) {
  return request.post<
    any,
    MainResponseType<
      buyListType<
        {
          transfer_id: number;
          amount_cost: number;
          amount_buy: number;
          cost_buy: number;
          buy_cost: number;
          receive_amount: number;
          transfer_sn: string;
          currency_cost: string;
          currency_buy: string;
          add_time: string;
          from_currency: string;
          from_creat_time: string;
          num: string;
        }[]
      >
    >
  >(URL.hangForSale, {
    nums,
    password,
    APITOKEN,
  });
}

export function buy_HangSalesMarketOrder(
  id: string,
  password: string,
  APITOKEN: string
) {
  return request.post<
    any,
    MainResponseType<
      buyListType<
        {
          transfer_id: number;
          amount_cost: number;
          amount_buy: number;
          cost_buy: number;
          buy_cost: number;
          receive_amount: number;
          transfer_sn: string;
          currency_cost: string;
          currency_buy: string;
          add_time: string;
          from_currency: string;
          from_creat_time: string;
          num: string;
        }[]
      >
    >
  >(URL.buy_HangSalesMarketOrder, {
    id,
    password,
    APITOKEN,
  });
}

export function get_HangSalesMarket(page: string) {
  return request.post<
    any,
    MainResponseType<
      buyListType<
        {
          transfer_id: number;
          amount_cost: number;
          amount_buy: number;
          cost_buy: number;
          buy_cost: number;
          receive_amount: number;
          transfer_sn: string;
          currency_cost: string;
          currency_buy: string;
          add_time: string;
          from_currency: string;
          from_creat_time: string;
          num: string;
        }[]
      >
    >
  >(URL.get_HangSalesMarket, {
    page,
  });
}

export function getTansferList(page: string) {
  return request.get<
    any,
    MainResponseType<
      buyListType<
        {
          transfer_id: number;
          amount_cost: number;
          amount_buy: number;
          cost_buy: number;
          buy_cost: number;
          receive_amount: number;
          transfer_sn: string;
          currency_cost: string;
          currency_buy: string;
          add_time: string;
          from_currency: string;
          from_creat_time: string;
          num: string;
          paynum: string;
          state: string;
        }[]
      >
    >
  >(URL.getTansferList, {
    params: {
      page,
    },
  });
}
export function getList(page: string) {
  return request.get<
    any,
    MainResponseType<
      buyListType<
        {
          transfer_id: number;
          amount_cost: number;
          receive_amount: number;
          transfer_sn: string;
          currency_cost: string;
          add_time: string;
        }[]
      >
    >
  >(URL.getList, {
    params: {
      page,
    },
  });
}
export function getApiToken() {
  return request.get<
    any,
    MainResponseType<{
      api_token: string;
    }>
  >(URL.getApiToken);
}
export enum swapEnum {
  "calculate" = "calculate",
  "swap" = "swap",
}
export function swap(data: {
  action: swapEnum;
  currency_cost: string;
  currency_buy: string;
  currency_main: string;
  amount: string;
  password?: string;
  APITOKEN?: string;
}) {
  return request.post<
    any,
    MainResponseType<{
      calculate: {
        error_code: number;
        currency_cost: string;
        amount_cost: number;
        currency_buy: string;
        amount_buy: number;
        cost_buy: number;
        buy_cost: number;
        amount_usdt: number;
        slippage_out: number;
        slippage_in: number;
        slippage_fee: number;
        receive_amount: number;
      };
      balance_status: boolean;
    }>
  >(URL.swap, data);
}
export function getCurrencyList(currency?: string) {
  return request.get<
    any,
    MainResponseType<{
      list_sale: {
        currency_id: number;
        min_buy: number;
        max_buy: number;
        currency: string;
        currency_name: string;
        currency_icon: string;
        slippage_out: string;
      }[];
      list_buy: {
        currency_id: number;
        min_buy: number;
        max_buy: number;
        currency: string;
        currency_name: string;
        currency_icon: string;
      }[];
    }>
  >(URL.getCurrencyList, {
    params: {
      currency,
    },
  });
}
